package com.letv.core.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.letv.core.bean.DownloadLocalVideoItemBean;
import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.LetvConstant;
import com.letv.core.utils.LetvTools;
import com.letv.core.utils.LogInfo;

/**
 * @author zhaosumin
 * 本地文件检索时使用
 *
 */
public class LocalVideoTraceHandler {
    private Context mContext;

    public LocalVideoTraceHandler(Context context) {
        this.mContext = context;
    }

    /**
     * 获得播放记录时间戳
     * 
     * @param path
     * @return
     */
    public long getPlayPosition(String path) {

        long position = -1;
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_LOCALVIDEOTRACE,
                    new String[] { DatabaseConstant.LocalVideoTrace.Field.POSITION },
                    DatabaseConstant.LocalVideoTrace.Field.PATH + "= ?", new String[] { path }, null);

            if (null != cursor) {

                if (cursor.moveToNext()) {

                    position = cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.POSITION));
                }
            }
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return position;

    }

    /**
     * 添加本地数据
     * 
     * @param file
     */
    public void addLocalVideo(DownloadLocalVideoItemBean item) {
        if (hasLocalVideo(item)) {
            LogInfo.log("pathToTitle", "updateByTitle = " + item.toString());
            updateByTitle(item);
        } else {
            LogInfo.log("pathToTitle", "insert = " + item.toString());
            ContentValues cv = new ContentValues();
            cv.put(DatabaseConstant.LocalVideoTrace.Field.TITLE, item.getTitle());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.PATH, item.getPath());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.POSITION, item.getPosition());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.CREATETIME, item.getFileDate());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.FILESIZE, item.getFileSize());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.TIMELENGTH, item.getFileDuration());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.VIDEO_W_H, item.getVideo_W_H());
            cv.put(DatabaseConstant.LocalVideoTrace.Field.VIDEO_TYPE, item.getVideoType());
            mContext.getContentResolver().insert(LetvContentProvider.URI_LOCALVIDEOTRACE, cv);
        }
    }

    /**
     * 更新数据表
     */
    private boolean updateByTitle(DownloadLocalVideoItemBean item) {
        try {
            if (item != null) {
                ContentValues cv = new ContentValues();
                cv.put(DatabaseConstant.LocalVideoTrace.Field.TITLE, item.getTitle());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.PATH, item.getPath());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.POSITION, item.getPosition());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.CREATETIME, item.getFileDate());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.FILESIZE, item.getFileSize());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.TIMELENGTH, item.getFileDuration());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.VIDEO_W_H, item.getVideo_W_H());
                cv.put(DatabaseConstant.LocalVideoTrace.Field.VIDEO_TYPE, item.getVideoType());

                mContext.getContentResolver().update(LetvContentProvider.URI_LOCALVIDEOTRACE, cv,
                        DatabaseConstant.LocalVideoTrace.Field.TITLE + "=?", new String[] { item.getTitle() });
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据title判断记录是否已存在
     * */
    public boolean hasLocalVideo(DownloadLocalVideoItemBean item) {
        if (item != null) {
            Cursor cursor = null;
            try {
                cursor = mContext.getContentResolver().query(LetvContentProvider.URI_LOCALVIDEOTRACE, null,
                        DatabaseConstant.LocalVideoTrace.Field.TITLE + "=?", new String[] { item.getTitle() },
                        null);
                int count = cursor.getCount();
                return count > 0;
            } finally {
                LetvTools.closeCursor(cursor);
            }
        } else {
            return false;
        }
    }

    /**
     * 添加本地数据
     * 
     * @param itemList
     */
    public void addLocalVideo(ArrayList<DownloadLocalVideoItemBean> itemList) {
        for (int i = 0; i < itemList.size(); i++) {
            addLocalVideo(itemList.get(i));
        }
    }

    /**
     * 更新播放记录
     * 
     * @param file
     * @param position
     */
    public void updateVideoPosition(String path, long position) {

        ContentValues cv = new ContentValues();

        cv.put(DatabaseConstant.LocalVideoTrace.Field.POSITION, position);

        mContext.getContentResolver().update(LetvContentProvider.URI_LOCALVIDEOTRACE, cv,
                DatabaseConstant.LocalVideoTrace.Field.PATH + "=?", new String[] { path });

    }

    /**
     * 更新播放记录
     * 
     * @param itemList
     */
    public void updateVideoPosition(ArrayList<DownloadLocalVideoItemBean> itemList) {
        for (int i = 0; i < itemList.size(); i++) {
            updateVideoPosition(itemList.get(i).getPath(), itemList.get(i).getPosition());
        }
    }

    /**
     * 获得含有Position的Video记录
     * 
     * @return
     */
    public ArrayList<DownloadLocalVideoItemBean> getLocalVideoHasPosition() {
        Cursor cursor = null;
        ArrayList<DownloadLocalVideoItemBean> itemList = new ArrayList<DownloadLocalVideoItemBean>();
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_LOCALVIDEOTRACE, null,
                    DatabaseConstant.LocalVideoTrace.Field.POSITION + " not in(?,?)",
                    new String[] { "-1000", "0" }, null);
            if (null != cursor) {
                while (cursor.moveToNext()) {
                    DownloadLocalVideoItemBean item = new DownloadLocalVideoItemBean();
                    item.setFileDate(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.CREATETIME)));
                    item.setFileDuration(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.TIMELENGTH)));
                    item.setFileSize(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.FILESIZE)));
                    item.setPath(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.PATH)));
                    item.setPosition(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.POSITION)));
                    item.setTitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.TITLE)));
                    item.setVideo_W_H(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.VIDEO_W_H)));
                    item.setVideoType(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.VIDEO_TYPE)));
                    itemList.add(item);
                }
            }
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return itemList;
    }

    /**
     * 获得所有本地视频记录
     */
    public ArrayList<DownloadLocalVideoItemBean> getLocalVideoList() {
        Cursor cursor = null;
        ArrayList<DownloadLocalVideoItemBean> itemList = new ArrayList<DownloadLocalVideoItemBean>();
        try {
            cursor = mContext.getContentResolver()
                    .query(LetvContentProvider.URI_LOCALVIDEOTRACE, null, null, null, null);
            if (null != cursor) {
                while (cursor.moveToNext()) {
                    DownloadLocalVideoItemBean item = new DownloadLocalVideoItemBean();
                    item.setFileDate(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.CREATETIME)));
                    item.setFileDuration(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.TIMELENGTH)));
                    item.setFileSize(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.FILESIZE)));
                    item.setPath(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.PATH)));
                    item.setPosition(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.POSITION)));
                    item.setTitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.TITLE)));
                    item.setVideo_W_H(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.VIDEO_W_H)));
                    item.setVideoType(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.LocalVideoTrace.Field.VIDEO_TYPE)));
                    itemList.add(item);
                }
            }
        } finally {
            LetvTools.closeCursor(cursor);
        }

        return itemList;
    }

    /**
     * 删除多条本地记录
     * 
     * @param itemList
     */
    public void delLocalVideoItem(ArrayList<DownloadLocalVideoItemBean> itemList) {
        for (int i = 0; i < itemList.size(); i++) {
            delLocalVideoItem(itemList.get(i));
        }
    }

    /**
     * 删除一条本地记录
     */
    public void delLocalVideoItem(DownloadLocalVideoItemBean item) {
        delLocalVideoItem(item.getPath());
    }

    /**
     * 删除一条本地记录
     */
    public void delLocalVideoItem(String path) {
        mContext.getContentResolver().delete(LetvContentProvider.URI_LOCALVIDEOTRACE,
                DatabaseConstant.LocalVideoTrace.Field.PATH + "=?", new String[] { path });
    }

    /**
     * 删除全部记录
     */
    public void delLocalVideoAll() {
        mContext.getContentResolver().delete(LetvContentProvider.URI_LOCALVIDEOTRACE, null, null);
    }
}
